package com.chijun.mform.create.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.R;
import com.chijun.mform.create.dialog.ISelectField;
import com.chijun.mform.create.field.Field;
import com.chijun.mform.create.field.FieldHandler;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;

import java.util.List;


/**
 * 加载字段类型
 * 点击字段后返回字段类型id
 * 2024年11月10日23:38:06
 */
public class SelectFieldTypeAdapter extends RecyclerView.Adapter<SelectFieldTypeAdapter.ViewHolder>{

    List<Field> fields ;
    ISelectField iSelectField;

    /**
     * 字段类型数组
     *
     * @param fields 字段列表
     * @param iSelectField 通过此回调返回选择的字段编号
     */
    public SelectFieldTypeAdapter(List<Field> fields, @NonNull ISelectField iSelectField){
        this.fields = fields;
        this.iSelectField = iSelectField;
    }

    @NonNull
    @Override
    public SelectFieldTypeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectFieldTypeAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapater_select_field_type, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectFieldTypeAdapter.ViewHolder holder, int position) {
        Field field = fields.get(position);
        holder.name.setText(field.name);//设置字段名
        holder.hint.setText(field.hint);//设置字段提示

        //设置字段图标
        if (!StringUtils.isEmpty(field.icon)) {
            ImageUtils.loadImage(holder.icon.getContext(),field.icon,holder.icon);
        } else {
            int fieldIcon = FieldHandler.getFieldIcon(field.id);
//            LogUtils.d("获取字段图标" + fieldIcon);
            if (fieldIcon > 0) ImageUtils.loadImage(holder.icon.getContext(),fieldIcon,holder.icon);
        }

        //设置字段是否可用
        if (!field.isEnable || !field.isAccess) {
            holder.lay.setEnabled(false);
        }

        //字段配置是否启用，如果启用设置点击回调
        holder.lay.setOnClickListener(v -> {
            if (field.isEnable && field.isAccess) {
                if (iSelectField != null) {
                    iSelectField.complete(field);
                }
            } else {
                //字段配置未启用该字段，弹出提示
                ToastUtils.showShort("该字段不可用！");
            }
        });
    }

    @Override
    public int getItemCount() {
        return fields == null ? 0 : fields.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final TextView name,hint;
        private final ImageView icon;
        private final LinearLayout lay;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            hint = itemView.findViewById(R.id.hint);
            icon = itemView.findViewById(R.id.icon);
            lay = itemView.findViewById(R.id.select_field_type_layout);
        }
    }
}
